package kr.pe.johnkim.app.mobile.rest.service;

import kr.pe.johnkim.app.domain.MyHistoryCategory;
import kr.pe.johnkim.app.domain.MyHistoryItem;
import kr.pe.johnkim.app.domain.MyHistoryMember;
import kr.pe.johnkim.app.mobile.rest.dao.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author : johnkim <cale9797@gmail.com / Bettersoft Inc,.>
 * @Date : 12. 1. 26
 */
//@Service(value= "myHistoryMemberService")
public class MyHistoryMemberServiceImpl implements MyHistoryMemberService {
    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(MyHistoryMemberServiceImpl.class);

//    @Autowired
//    private MyHistoryMemberDaoImpl myHistoryMemberDao = null;
//    @Autowired
//    private MyHistoryCommon common = null;
//    @Autowired
//    private MyHistoryEmail myHistoryEmail = null;
//    @Autowired
//    private MyHistoryGroupDaoImpl myHistoryGroupDao = null;

    /**
     * 멤버 등록시 중복 확인을 한다.
     *
     * @param myHistoryMember
     * @return MyHistoryMember
     */
    @Override
    public MyHistoryMember checkDuplication(MyHistoryMember myHistoryMember) {
        return myHistoryMemberDao.checkDuplication(myHistoryMember);
    }

    /**
     * 멤버를 등록 한다.
     *
     * 1. 사용자 정보를 등록 한다.
     * 2. 미리 정의된 Group 목록을 등록 한다.
     * 3. 등록된 Group 목록을 가져 온다.
     * 4. Group 목록의 id, name 으로 미리 정의된 Category 목록을 등록 한다.
     * 5. 등록된 Category 목록을 가져 온다.
     * 6. Category 목록의 id 로 미리 정의된 Item 목록을 등록 한다.
     *
     * 총 68번의 connection(insert, select) 이 발생 한다.
     *
     * @param myHistoryMember
     * @return True or False
     */
    @Override
    public boolean registerMember(MyHistoryMember myHistoryMember)  {
        logger.info("registerMember");
        
        boolean result = false;

        try {
            // 사용자를 등록 한다.
            myHistoryMemberDao.registerMember(myHistoryMember);

            /**
             * 미리 정의된 Category 목록을 등록 한다.
             */
            MyHistoryCategory myHistoryCategory = new MyHistoryCategory();

            myHistoryCategory.setOpenCategoryYn("N");
            myHistoryCategory.setFavoriteYn("N");
            myHistoryCategory.setActivateYn("Y");
            myHistoryCategory.setModifyDate(myHistoryMember.getModifyDate());
            myHistoryCategory.setRegisterDate(myHistoryMember.getRegisterDate());
            myHistoryCategory.setMyHistoryMembersId(myHistoryMember.getId());

            logger.info("openCategoryYn     : {}", myHistoryCategory.getOpenCategoryYn());
            logger.info("favoriteYn         : {}", myHistoryCategory.getFavoriteYn());
            logger.info("activateYn         : {}", myHistoryCategory.getActivateYn());
            logger.info("modifyDate         : {}", myHistoryCategory.getModifyDate());
            logger.info("registerDate       : {}", myHistoryCategory.getRegisterDate());
            logger.info("myHistoryMembersId : {}", myHistoryCategory.getMyHistoryMembersId());


            myHistoryCategoryDao.registerCategoryList(myHistoryCategory);

            /**
             * Category 목록 등록이 완료 되면, Item 목록을 등록 한다.
             */
            List<MyHistoryCategory> categoryList = myHistoryCategoryDao.selectCategoryListForRegisterItemList(myHistoryCategory);

            // Item 도메인 클래스를 세팅 한다.
            MyHistoryItem myHistoryItem = new MyHistoryItem();

            myHistoryItem.setOpenItemYn("N");
            myHistoryItem.setFavoriteYn("N");
            myHistoryItem.setActivateYn("Y");
            myHistoryItem.setModifyDate(myHistoryMember.getModifyDate());
            myHistoryItem.setRegisterDate(myHistoryMember.getRegisterDate());
            myHistoryItem.setMyHistoryMembersId(myHistoryMember.getId());

            // 각 Category 에 Item 목록을 등록 한다.
            for(int i=0 ; i<categoryList.size() ; i++) {
                myHistoryItem.setMyHistoryCategoriesIdMyHistoryCategories(categoryList.get(i).getIdMyHistoryCategories().toString());

                if(("커피").equals(categoryList.get(i).getName())) {

                    myHistoryItemDao.registerItemListForCoffeeCategory(myHistoryItem);
                    logger.info("registerItemListForCoffeeCategory");

                } else if(("주류").equals(categoryList.get(i).getName())) {

                    myHistoryItemDao.registerItemListForAlcoholCategory(myHistoryItem);
                    logger.info("registerItemListForAlcoholCategory");

                }
            }

            result = true;

            logger.info("Success to register member and group list.");
        } catch (Exception e) {
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

            result = false;
            
            logger.info("Fail to register member and group list.");

//            throw new RuntimeException();
        } 

        return result;  //To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * 멤버 로그인을 한다.
     *
     * @param myHistoryMember
     * @return MyHistoryMember
     */
    @Override
    public MyHistoryMember login(MyHistoryMember myHistoryMember) {
        logger.info("login");
        return myHistoryMemberDao.login(myHistoryMember);
    }

    /**
     * 멤버 정보를 수정 한다.
     *
     * @param myHistoryMember
     * @return
     */
    @Override
    public boolean updateMember(MyHistoryMember myHistoryMember) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * 멤버를 비활성화 한다.
     *
     * @param myHistoryMember
     * @return
     */
    @Override
    public boolean updateDeactivateMember(MyHistoryMember myHistoryMember) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * 사용자의 마일리지를 가져 온다.
     *
     * @param myHistoryMember
     * @return
     */
    @Override
    public MyHistoryMember selectMileage(MyHistoryMember myHistoryMember) {
        logger.info("selectMileage");
        return myHistoryMemberDao.selectMileage(myHistoryMember);
    }

    /**
     * 마일리지를 업데이트(추가) 한다.
     *
     * @param myHistoryMember
     * @return
     */
    @Override
    public boolean updateMileage(MyHistoryMember myHistoryMember) {
        logger.info("updateMileage");
        return myHistoryMemberDao.updateMileage(myHistoryMember);
    }

    /**
     * 사용자 비밀번호를 변경 한다.
     *
     * @param myHistoryMember
     * @return
     */
    @Override
    public boolean updatePassword(MyHistoryMember myHistoryMember) {
        logger.info("updatePassword");
        return myHistoryMemberDao.updatePassword(myHistoryMember);
    }

    /**
     * 사용자 이메일을 업데이트 한다.
     *
     * @param myHistoryMember
     * @return
     */
    @Override
    public boolean updateEmail(MyHistoryMember myHistoryMember) {
        logger.info("updateEmail");
        return myHistoryMemberDao.updateEmail(myHistoryMember);
    }

    /**
     * Spring Setter Injection.
     */
    private MyHistoryMemberDao myHistoryMemberDao;
    private MyHistoryCategoryDao myHistoryCategoryDao;
    private MyHistoryItemDao myHistoryItemDao;

    public void setMyHistoryMemberDao(MyHistoryMemberDaoImpl myHistoryMemberDao) {
        this.myHistoryMemberDao = myHistoryMemberDao;
    }

    public void setMyHistoryCategoryDao(MyHistoryCategoryDaoImpl myHistoryCategoryDao) {
        this.myHistoryCategoryDao = myHistoryCategoryDao;
    }

    public void setMyHistoryItemDao(MyHistoryItemDaoImpl myHistoryItemDao) {
        this.myHistoryItemDao = myHistoryItemDao;
    }
}
